﻿<cfsilent>
	<cfscript>

		sessionAdvice = getProperty("serviceFactory").getBean("sessionAdvice");
		mathAdvice = getProperty("serviceFactory").getBean("senateMathAdvice");

		termId = event.getArg("TermID", event.getArg("TID"));

		/* 学籍相关信息 */
		studentId = event.getArg("STU");
		
		/* 读取学生已选课程的排课记录 */
		sql = "	SELECT
					c.course_name,
					tt.tasktime,
					CASE WHEN tt.taskclsroom IS NULL THEN '-' ELSE tt.taskclsroom END taskclsroom,
					tt.task_week,
					ta.task_type,ta.task_id
				FROM
					t_task_timetable tt
					INNER JOIN t_task_arrange ta ON tt.task_id = ta.task_id
					INNER JOIN t_course c ON ta.cid = c.cid
				WHERE
					EXISTS(
						SELECT 0 FROM t_student_course sc
						WHERE 
							ta.task_id = sc.task_id
							AND
							ta.term_id = :termId 
							AND
							sc.stu_id = :studentId 
					)";
		
		queryObj = new Query(datasource = application.dnsSlave);
		queryObj.addParam(name = "termId", value = termId, cfsqltype = "cf_sql_char");
		queryObj.addParam(name = "studentId", value = studentId, cfsqltype = "cf_sql_varchar");
		rs_choosenData = queryObj.execute(sql = sql).getResult();

		

		timeTable = arrayNew(2);
		
		/* 课表冲突标记 */
		for ( lec=1; lec lte 6; lec++ ) {
			for ( wkday=1; wkday lte 7; wkday++ ) {
				timeTable[wkday][lec] = 0;
			}
		}
		
		
		timeTableSet = structNew();
		
		/* 生成课表显示数据 */
		//for ( rs in choosenData ) {
		for(a = 1; a LTE rs_choosenData.recordCount; a++){
			
			currentTaskTime = rs_choosenData["tasktime"][a];
			
			if ( rs_choosenData["taskclsroom"][a] eq "-" ) { 
				currentTaskPlace = "";
			}
			else {
				currentTaskPlace = rs_choosenData["taskclsroom"][a];
			}
			
			wkday = listGetAt(currentTaskTime, 1, "-@");
			lec = listGetAt(currentTaskTime, 2, "-@");
			cell = listGetAt(currentTaskTime, 3, "-@");
			
			/* 载入教学任务 */
			switch( rs_choosenData['task_type'][a] ){
					
				case 'P':
					sql = "	SELECT
								tch.tch_name,tch_sec.tch_name tch_name_sec,tch_ace.tch_name tch_name_ace
							FROM
								t_task t
								INNER JOIN t_teacher tch ON t.tch_id = tch.tch_id
								INNER JOIN t_teacher tch_sec ON t.tch_id_sec = tch_sec.tch_id
								INNER JOIN t_teacher tch_ace ON t.tch_id_ace = tch_ace.tch_id
							WHERE
								t.tsk_id = :taskId ";
					queryObj = new Query(datasource = application.dnsSlave);
					queryObj.addParam(name = "taskId", value = rs_choosenData['task_id'][a], cfsqltype="cf_sql_varchar");
					rs_taskEntity = queryObj.execute(sql = sql).getResult();
					break;
				case 'C':
					sql = "	SELECT
								tch.tch_name,tch_sec.tch_name tch_name_sec,tch_ace.tch_name tch_name_ace
							FROM
								t_common_task t
								INNER JOIN t_teacher tch ON t.tch_id = tch.tch_id
								INNER JOIN t_teacher tch_sec ON t.tch_id_sec = tch_sec.tch_id
								INNER JOIN t_teacher tch_ace ON t.tch_id_ace = tch_ace.tch_id
							WHERE
								t.tsk_id = :taskId ";
					queryObj = new Query(datasource = application.dnsSlave);
					queryObj.addParam(name = "taskId", value = rs_choosenData['task_id'][a], cfsqltype="cf_sql_varchar");
					rs_taskEntity = queryObj.execute(sql = sql).getResult();
					break;
				case 'R':
					sql = "	SELECT
								tch.tch_name,tch_sec.tch_name tch_name_sec,tch_ace.tch_name tch_name_ace
							FROM
								t_reopen_task t
								INNER JOIN t_teacher tch ON t.tch_id = tch.tch_id
								INNER JOIN t_teacher tch_sec ON t.tch_id_sec = tch_sec.tch_id
								INNER JOIN t_teacher tch_ace ON t.tch_id_ace = tch_ace.tch_id
							WHERE
								t.tsk_id = :taskId ";
					queryObj = new Query(datasource = application.dnsSlave);
					queryObj.addParam(name = "taskId", value = rs_choosenData['task_id'][a], cfsqltype="cf_sql_varchar");
					rs_taskEntity = queryObj.execute(sql = sql).getResult();
					break;
				case 'T':
					sql = "	SELECT
								tch.tch_name,tch_sec.tch_name tch_name_sec,tch_ace.tch_name tch_name_ace
							FROM
								t_sport_task t
								INNER JOIN t_teacher tch ON t.tch_id = tch.tch_id
								INNER JOIN t_teacher tch_sec ON t.tch_id_sec = tch_sec.tch_id
								INNER JOIN t_teacher tch_ace ON t.tch_id_ace = tch_ace.tch_id
							WHERE
								t.tsk_id = :taskId ";
					queryObj = new Query(datasource = application.dnsSlave);
					queryObj.addParam(name = "taskId", value = rs_choosenData['task_id'][a], cfsqltype="cf_sql_varchar");
					rs_taskEntity = queryObj.execute(sql = sql).getResult();
					break;
			}
			/* 生成教师 */
			currentTeacher = rs_taskEntity.tch_name;
			
			if ( not listFind( currentTeacher, rs_taskEntity.tch_name_sec, "/ " ) ) {
				currentTeacher = currentTeacher & " / " & rs_taskEntity.tch_name_sec;
			}
			
			if ( not listFind( currentTeacher, rs_taskEntity.tch_name_ace, "/ " ) ) {
				currentTeacher = currentTeacher & " / " & rs_taskEntity.tch_name_ace;
			}
			
			/* 
				生成课表内容 
				cellCourse = [ courseName, teacherName, place, cellLength, taskWeek]
			*/
			if ( cell eq 2 ) {

				cellCourse = [ rs_choosenData["course_name"][a], currentTeacher, currentTaskPlace, cell, rs_choosenData["task_week"][a] ];
			
				lectureKey = "KB" & wkday & lec;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec] = 1;
						}
					}

					arrayAppend( timeTableSet[lectureKey], cellCourse );
					
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}
				
			}
			
			if ( cell eq 3 and lec eq 2 ) {

				cellCourse = [ rs_choosenData["course_name"][a], currentTeacher, currentTaskPlace, cell, rs_choosenData["task_week"][a] ];
			
				lectureKey = "KB" & wkday & lec;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec] = 1;
						}
					}

					arrayAppend( timeTableSet[lectureKey], cellCourse );
					
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}
				
			}
			
			if ( cell eq 3 and lec eq 5 ) {

				cellCourse = [ rs_choosenData["course_name"][a], currentTeacher, currentTaskPlace, 2, rs_choosenData["task_week"][a] ];
			
				lectureKey = "KB" & wkday & lec;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec] = 1;
						}
					}

					arrayAppend( timeTableSet[lectureKey], cellCourse );
					
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}
				
				/* 晚上3节延续1学时 */
				cellCourse = [ rs_choosenData["course_name"][a], currentTeacher, currentTaskPlace, 1, rs_choosenData["task_week"][a] ];
			
				lectureKey = "KB" & wkday & lec+1;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec+1] = 1;
						}
					}

					arrayAppend( timeTableSet[lectureKey], cellCourse );
					
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}
				
			}
			
			/* 4学时延续 */
			if ( cell eq 4 ) {
				
				cellCourse = [ rs_choosenData["course_name"][a], currentTeacher, currentTaskPlace, 2, rs_choosenData["task_week"][a] ];
				
				lectureKey = "KB" & wkday & lec;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec] = 1;
						}
					}
					
					arrayAppend( timeTableSet[lectureKey], cellCourse );
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}
				
				lectureKey = "KB" & wkday & lec+1;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec+1] = 1;
						}
					}
					
					arrayAppend( timeTableSet[lectureKey], cellCourse );
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}
			}
			
			/* 5学时延续 */
			if ( cell eq 5 ) {
				
				cellCourse = [ rs_choosenData["course_name"][a], currentTeacher, currentTaskPlace, 2, rs_choosenData["task_week"][a] ];
				
				lectureKey = "KB" & wkday & lec;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec] = 1;
						}
					}
					
					arrayAppend( timeTableSet[lectureKey], cellCourse );
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}

				cellCourse = [ rs_choosenData["course_name"][a], currentTeacher, currentTaskPlace, 3, rs_choosenData["task_week"][a] ];
				
				lectureKey = "KB" & wkday & lec+1;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec+1] = 1;
						}
					}
					
					arrayAppend( timeTableSet[lectureKey], cellCourse );
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}
			}
			
			/* 8学时延续 */
			if ( cell eq 8 ) {
				
				cellCourse = [ rs_choosenData["course_name"][a], currentTeacher, currentTaskPlace, 2, rs_choosenData["task_week"][a] ];
				
				lectureKey = "KB" & wkday & lec;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec] = 1;
						}
					}
					
					arrayAppend( timeTableSet[lectureKey], cellCourse );
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}
			
				lectureKey = "KB" & wkday & lec+1;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec+1] = 1;
						}
					}
					
					arrayAppend( timeTableSet[lectureKey], cellCourse );
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}

				lectureKey = "KB" & wkday & lec+2;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec+2] = 1;
						}
					}
					
					arrayAppend( timeTableSet[lectureKey], cellCourse );
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}

				lectureKey = "KB" & wkday & lec+3;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec+3] = 1;
						}
					}
					
					arrayAppend( timeTableSet[lectureKey], cellCourse );
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}

			}

		}
		
		
	</cfscript>
</cfsilent>

<table class="UICourseTable">
	<thead>
		<tr>
			<td colspan="2" width="2%"></td>
			<td width="14%">星期一</td>
			<td width="14%">星期二</td>
			<td width="14%">星期三</td>
			<td width="14%">星期四</td>
			<td width="14%">星期五</td>
			<td width="14%">星期六</td>
			<td width="14%">星期天</td>
		</tr>
	</thead>
	<tbody>
		<cfloop from="1" to="6" index="lec">
			<tr>
				<cfswitch expression="#lec#">
					<cfcase value="1">
					<td rowspan="2"><cfoutput>#mathAdvice.convertLectureIdxToDayPart(lec)#</cfoutput></td>
					</cfcase>
					<cfcase value="3">
						<td rowspan="2"><cfoutput>#mathAdvice.convertLectureIdxToDayPart(lec)#</cfoutput></td>
					</cfcase>
					<cfcase value="5">
						<td rowspan="2"><cfoutput>#mathAdvice.convertLectureIdxToDayPart(lec)#</cfoutput></td>
					</cfcase>
				</cfswitch>
				<td><cfoutput>#mathAdvice.convertLectureIdxToChinese(lec)#</cfoutput></td>
				<cfloop from="1" to="7" index="wkday">
					<cfset lectureKey = "KB" & wkday & lec />
					<td <cfif timeTable[wkday][lec]>class="attention"</cfif>>
						<cfif structKeyExists( timeTableSet, lectureKey )>
							<cfset i = 0 />
							<cfloop array="#timeTableSet[lectureKey]#" index="cellCourse">
								<cfset i++ />
								<div class="lecture">
									<span class="course"><cfoutput>#cellCourse[1]#</cfoutput></span> - <span class="teacher"><cfoutput>#cellCourse[2]#</cfoutput></span>
									<br/>
									<span class="week"><cfoutput>#cellCourse[5]#</cfoutput>(<cfoutput>#cellCourse[4]#</cfoutput>)</span>
									<cfif cellCourse[3] neq "-">
										<br/>
										<span class="place"><cfoutput>#cellCourse[3]#</cfoutput></span>
									</cfif>
								</div>
								<cfif i lt arrayLen(timeTableSet[lectureKey]) >
									<div class="cellBreaker"><!--.--></div>
								</cfif>
							</cfloop>
						</cfif>
					</td>
				</cfloop>
			</tr>
		</cfloop>
	</tbody>
</table>